﻿using System.ComponentModel;
using System.Diagnostics;
using System.Threading;
using System.Windows;
using System.Windows.Media;
using MC.Phone.Ads.Provider;
using MC.Phone.Ads.Utils;
using MillennialMedia.WP7.SDK;

namespace MC.Phone.Ads.MillennialMedia
{
    public enum AdType
    {
        Bottom,
        Top
    }

    public class MillennialMediaAdProvider : BaseAdProvider
    {
        private WeakEventListener<MillennialMediaAdProvider, MMAdView, MMException> _adFailureListener;

        private WeakEventListener<MillennialMediaAdProvider, MMAdView> _adSuccessListener;


        public override string Name
        {
            get { return "MillennialMedia"; }
        }

        public static readonly DependencyProperty ApidProperty =
            DependencyProperty.Register("Apid", typeof (string), typeof (MillennialMediaAdProvider), null);

        [Category("Ad")]
        public string Apid
        {
            get { return (string) GetValue(ApidProperty); }
            set { SetValue(ApidProperty, value); }
        }

        public static readonly DependencyProperty AdTypeProperty =
            DependencyProperty.Register("AdType", typeof (AdType), typeof (MillennialMediaAdProvider), new PropertyMetadata(default(AdType)));

        [Category("Ad")]
        public AdType AdType
        {
            get { return (AdType) GetValue(AdTypeProperty); }
            set { SetValue(AdTypeProperty, value); }
        }

        private MMAdType Convert(AdType type)
        {
            switch (type)
            {
                case AdType.Top:
                    return MMAdType.MMBannerAdTop;
                case AdType.Bottom:
                default:
                    return MMAdType.MMBannerAdBottom;
            }
        }

        public override FrameworkElement ProvideAd()
        {
            var control = new MMBannerAdView
                              {
                                  BackgroundColor =
                                      (Application.Current.Resources["PhoneBackgroundBrush"] as SolidColorBrush).Color,
                                  RefreshTimer = 60,
                                  Height = 60,
                                  Width = 480,
                                  AdType = Convert(AdType)
                              };
            _adFailureListener = new WeakEventListener<MillennialMediaAdProvider, MMAdView, MMException>(this)
                                     {
                                         OnEventAction = (instance, source, args) => instance.ControlAdFailure(),
                                         OnDetachAction = weak => { }
                                     };
            _adSuccessListener = new WeakEventListener<MillennialMediaAdProvider, MMAdView>(this)
                                     {
                                         OnEventAction = (instance, source) => instance.ControlAdSuccess(),
                                         OnDetachAction = weak => { }
                                     };
            control.GetAd(Apid, _adSuccessListener.OnEvent, _adFailureListener.OnEvent);

            return control;
        }

        public override void Clean()
        {
            _adFailureListener = _adFailureListener.DetachToNull();
            _adSuccessListener = _adSuccessListener.DetachToNull();
        }

        private void ControlAdSuccess()
        {
            ExecuteOnDispatcher(OnNewAd);
        }

        private void ControlAdFailure()
        {
            ExecuteOnDispatcher(OnNoAd);
        }

#if DEBUG
        ~MillennialMediaAdProvider()
        {
            Debug.WriteLine( Thread.CurrentThread.ManagedThreadId + "--Finalizing " + GetType().FullName);
        }
#endif
    }
}